﻿package chapter_07_Homework;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Cal {
	private static Logger logger = Logger.getLogger(Cal.class.getName());
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true){
			try{
				System.out.print("请输入第一个数:");
				int num1 = sc.nextInt();
				
				System.out.print("请输入第二个数:");
				int num2 = sc.nextInt();
				
				System.out.print("请选择你要进行的运算功能(+ - * /):");
				try{
					String choose = sc.next(); 
					
					switch(choose){
					case "+":
						System.out.println(String.format("%d+%d=%d", num1,num2,num1+num2));
						break;
					case "-":
						System.out.println(String.format("%d-%d=%d", num1,num2,num1-num2));
						break;
					case "*":
						System.out.println(String.format("%d×%d=%d", num1,num2,num1*num2));
						break;
					case "/":
						if(num2!=0){
							System.out.println(String.format("%d÷%d=%d", num1,num2,num1/num2));
							break;
						}else{
							System.err.println("除数不能为0！");
						}
					}
				}catch(Exception e){
					System.err.println("系统无法识别该功能，请重新选择！");
				}
			}catch(InputMismatchException e){
				System.err.println("必须为合法数字！");
				System.exit(1);
			}catch(Exception e){
				System.err.println("程序遇到未知错误，请关闭后重试！");
				System.exit(1);
			}
			System.out.println("是否继续？(y/n)");
			if(sc.hasNext()){
				String answer = sc.next();
				if(!"y".equals(answer)){
					System.out.println("感谢使用！");
					break;
				}
			}
		}
	}
}
